import time
import datetime
import random
from time import sleep


# 获取当前时间为当天多少秒
def get_second(add_minute=0):
    time = datetime.datetime.now()
    hour_time = time.strftime('%H')
    minute = int(time.strftime('%M')) + add_minute
    base_time = int(hour_time) * 3600 + int(minute) * 60
    return base_time


# 获取当前时间的时间戳
def time1():
    return int(time.time())


def time_str1():
    time_now = int(time.time())
    return str(time_now)


# 获取当前时间的时间戳,时间戳是str类型
def time_str():
    return str(time.time())


def get_future_timestamp(day=0, num=13):
    """
    获取多少天后的时间戳，多少位的时间戳，默认0天13位
    :param day: 多少天后
    :param num: 多少位时间戳
    :return: 时间戳
    """
    try:
        now = datetime.date.today()
        day = datetime.timedelta(days=day)
        base_day = now + day
        date = base_day.strftime('%Y-%m-%d')
        date_time = time.strptime(date, '%Y-%m-%d')
        timestamp = time.mktime(date_time)
        # 计算多少位时间戳
        if num <= 13:
            base_time = 10 ** (num - 13) * 1000
        base_timestamp = str((timestamp * base_time)).split('.')[0]
        return base_timestamp
    except Exception as error:
        print(error)
        return 0


# 获取指定时间的时间戳
def get_timestamp(date=None):
    if not date:
        return int(time.time())
    timestamp = 0
    try:
        date_time = time.strptime(date, '%Y-%m-%d %H:%M:%S')
        timestamp = time.mktime(date_time)
    except Exception as e:
        return 0
    return int(timestamp)


# 获取指定时间的时间戳,单位毫秒
def get_timestampms(date=None):
    if not date:
        return int(time.time())
    timestamp = 0
    try:
        date_time = time.strptime(date, '%Y-%m-%d %H:%M:%S')
        timestamp = time.mktime(date_time)
    except Exception as e:
        return 0
    return int(timestamp * 1000)


# 今天的日期
def time_today():
    return datetime.date.today()


# 今天的日期 精确到日
def time_today0():
    return datetime.datetime.now().strftime('%Y-%m-%d')


def time_today00():
    return datetime.datetime.now().strftime('%Y%m%d')


def time_today01():
    return datetime.datetime.now().strftime('%Y/%m/%d')


# 今天的日期 精确到秒
def time_today1():
    return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')


# 今天的日期 精确到分
def time_today_m():
    return datetime.datetime.now().strftime('%Y-%m-%d %H:%M')


# 今天的日期  时间戳7位
def time_today3():
    return int(round(time.time() / 1000))


# 今天的日期  时间戳10位
def time_today4():
    return int(round(time.time()))


def time_today_y():
    return datetime.datetime.now().strftime('%m-%d %H:%M')


# 今天的日期  时间戳 n=0，10位时间戳；n=13，13位时间戳；n>0 and n!=13，自定义位数时间戳
def get_today_timestamp(n):
    if n == 0:
        return int(round(time.time()))
    elif n > 0 and n != 13:
        return int(round(time.time() / n))
    elif n == 13:
        return int(round(time.time() * 1000))


def time_today46():
    return str(round(time.time()))


# 获取毫秒时间戳-pa
def time_minisecond():
    return int(time.time() * 1000)


# 获取当前时间戳往后延2秒
def time_minisecond2():
    timeNow = time.time() * 1000 + 2000
    return int(timeNow)


# 输入的毫秒值转换成秒级时间
def milionTime_string(timeNum):
    timeStamp = float(timeNum / 1000)
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    return otherStyleTime


# 今天的日期  时间戳7位
def time_today5():
    return int(round(time.time() / 100000))


# 今天的n天后的日期
def getDelta(days_count):
    now = datetime.datetime.now()
    delta = datetime.timedelta(days=days_count)
    n_days = now + delta
    return n_days.strftime('%Y-%m-%d %H:%M:%S')


# 今天的n天前的日期
def getBeafer(days_count):
    now = datetime.datetime.now()
    delta = datetime.timedelta(days=days_count)
    n_days = now - delta
    return n_days.strftime('%Y-%m-%d %H:%M:%S')


# 今天的n天后的日期,精确到日
def getDelta0(days_count):
    now = datetime.datetime.now()
    delta = datetime.timedelta(days=days_count)
    n_days = now + delta
    return n_days.strftime('%Y-%m-%d')


# 今天的n天后的日期,精确到日，格式20200520
def getDelta1(days_count):
    now = datetime.datetime.now()
    delta = datetime.timedelta(days=days_count)
    n_days = now + delta
    return n_days.strftime('%Y%m%d')


def getDelta00(days_count):
    now = datetime.datetime.now()
    delta = datetime.timedelta(days=days_count)
    n_days = now + delta
    return n_days.strftime('%Y%m%d')


# 获取当前时间多少小时后的日期
def getDateHours(hour_count=0):
    new = datetime.datetime.now()
    hour = datetime.timedelta(hours=hour_count)
    base = new + hour
    base_data = base.strftime("%Y-%m-%d %H:%M:%S")
    return base_data


# 获取当前时间多少分钟后的日期
def getDateMinutes(minutes_count=0):
    new = datetime.datetime.now()
    minutes = datetime.timedelta(minutes=minutes_count)
    base = new + minutes
    base_data = base.strftime("%Y-%m-%d %H:%M:%S")
    return base_data


# 获取当前时间多少天后的日期
def getDateDays(days=0):
    new = datetime.datetime.now()
    days = datetime.timedelta(days=days)
    base = new + days
    base_data = base.strftime("%Y-%m-%d %H:%M:%S")
    return base_data


# 获取当前时间多少天,多少小时后的日期
def getDateDaysHours(days=0, hours=0):
    new = datetime.datetime.now()
    days = datetime.timedelta(days=days)
    hour = datetime.timedelta(hours=hours)
    base = new + days + hour
    base_data = base.strftime("%Y-%m-%d %H:%M:%S")
    return base_data


# 获取当天毫秒级时间戳
def date_timestamp():
    dt = datetime.date.today()
    return int(time.mktime(time.strptime(str(dt), "%Y-%m-%d"))) * 1000


# 时分秒时间戳，用于智能排课-上课开始时间和结束时间
def beginTimeOrendTime(minute_count):
    t = datetime.datetime.now().strftime('%H:%M:%S')
    h, m, s = t.strip().split(":")
    return int(h) * 3600 + (int(m) + int(minute_count)) * 60 + int(s)


# 等待时间
def wait_time(time=1):
    a = sleep(time)
    return a


# 今天之后1-20天随机取一天的日期,精确到日，格式20200520
def getDelta_random():
    now = datetime.datetime.now()
    days_count = random.randint(1, 20)
    delta = datetime.timedelta(days=days_count)
    n_days = now + delta
    return n_days.strftime('%Y%m%d')


def getDelta_random1():
    now = datetime.datetime.now()
    days_count = random.randint(10, 15)
    delta = datetime.timedelta(days=days_count)
    n_days = now + delta
    return n_days.strftime('%Y%m%d')


def getDelta_random11():
    now = datetime.datetime.now()
    days_count = random.randint(1, 1)
    delta = datetime.timedelta(days=days_count)
    n_days = now + delta
    return n_days.strftime('%Y-%m-%d')


# 当前的时间,精确到秒，格式20200715150349
def time_today21():
    return datetime.datetime.now().strftime('%Y%m%d%H%M%S')


# ---------------时间戳(毫秒)转换成字符串格式:"2013-10-10 23:40:00"--------------------------
def time_change_str(timestamp):
    time_local = time.localtime(timestamp / 1000)
    return time.strftime("%Y-%m-%d %H:%M:%S", time_local)